#include "stdafx.h"
#include "IExampleExe.h"
#include "MyClassFactory.h"

void Trace(LPCTSTR format, ...)
{
    va_list ap;
    va_start(ap, format);
    TCHAR sz[256];
    _vstprintf_s(sz, _countof(sz), format, ap);
    va_end(ap);
    OutputDebugString(sz);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
    HRESULT hr;
    hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        Trace(_T("CoInitialize failed with error code: %x\n"), hr);
        return 1;
    }

    DWORD token;
    hr = CoRegisterClassObject(CLSID_IExampleExe, &ClassFactory, CLSCTX_SERVER, REGCLS_MULTIPLEUSE, &token);
    if (FAILED(hr))
    {
        Trace(_T("CoRegisterClassObject failed with error code: %x\n"), hr);
        return 2;
    }

    MSG msg;
    while (GetMessage(&msg, 0, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    if (ExampleExeTypeInfo != NULL) ExampleExeTypeInfo->Release();

    CoRevokeClassObject(token);
    CoUninitialize();
    return 0;
}
